package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.script.tempenum.DemoEnum;
import com.xbongbong.saas.script.tempenum.DemoIconEnum;
import com.xbongbong.saas.script.tempenum.TempContactEnum;
import com.xbongbong.saas.script.tempenum.TempCustomerManagementEnum;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * DEMO
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:42
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/formDemo")
public class DemoController {

    @Resource
    PaasFormExplainModel paasFormExplainModel;
    /**
     * saas表单新建模拟
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/initDemo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void initDemo() throws Exception {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByKey(2115,"1");

        List<DemoEnum> demoEnumList = DemoEnum.getAllEnum();
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        for (DemoEnum demoEnum : demoEnumList){
            FieldAttrEntity fieldAttrEntity = InitExplainsHelper.setField(demoEnum);
            String attr = demoEnum.getSaasAttr().substring(0, 1).toUpperCase() + demoEnum.getSaasAttr().substring(1);
            try {
                Method method = DemoEnum.class.getMethod("init" +attr,FieldAttrEntity.class);
                method.invoke(demoEnum,fieldAttrEntity);
            }catch (Exception e){

            }
            fieldAttrEntityList.add(fieldAttrEntity);
        }

        paasFormExplainEntity.setExplains(JSONArray.toJSONString(fieldAttrEntityList));
        paasFormExplainModel.update(paasFormExplainEntity);

    }


    @RequestMapping(value = "/initCustomer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void initCustomer() throws Exception {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByKey(2115,"1");

        List<TempCustomerManagementEnum> demoEnumList = TempCustomerManagementEnum.getAllEnum();
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        for (TempCustomerManagementEnum demoEnum : demoEnumList){
            if (!(demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.TEXT.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.COMBO.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.DATETIME.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.ADDRESS.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.SUB_FORM.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.BIRTHDAY.getType()) ||
                    demoEnum.getAnEnum().getFieldType().equals(FieldTypeEnum.STAR.getType())
            )){
                continue;
            }
            FieldAttrEntity fieldAttrEntity = InitExplainsHelper.setField(demoEnum.getAnEnum(),RedundantTemplateTypeEnum.CUSTOMER.getCode());
            String saasAttr = fieldAttrEntity.getSaasAttr();
            String attr = saasAttr.substring(0, 1).toUpperCase() + saasAttr.substring(1);
            try {
                Method method = TempCustomerManagementEnum.class.getMethod("init" +attr,FieldAttrEntity.class);
                method.invoke(demoEnum,fieldAttrEntity);
            }catch (Exception e){

            }
            fieldAttrEntityList.add(fieldAttrEntity);
        }

        paasFormExplainEntity.setExplains(JSONArray.toJSONString(fieldAttrEntityList));
        paasFormExplainModel.update(paasFormExplainEntity);

    }

    @RequestMapping(value = "/initContact", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void initContact() throws Exception {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByKey(2136,"1");

        List<TempContactEnum> demoEnumList = TempContactEnum.getAllEnum();
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        for (TempContactEnum demoEnum : demoEnumList){
            FieldAttrEntity fieldAttrEntity = InitExplainsHelper.setField(demoEnum.getAnEnum(),RedundantTemplateTypeEnum.CONTACT.getCode());
            String saasAttr = fieldAttrEntity.getSaasAttr();
            String attr = saasAttr.substring(0, 1).toUpperCase() + saasAttr.substring(1);
            try {
                Method method = TempContactEnum.class.getMethod("init" +attr,FieldAttrEntity.class);
                method.invoke(demoEnum,fieldAttrEntity);
            }catch (Exception e){

            }
            fieldAttrEntityList.add(fieldAttrEntity);
        }

        paasFormExplainEntity.setExplains(JSONArray.toJSONString(fieldAttrEntityList));
        paasFormExplainModel.update(paasFormExplainEntity);

    }

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectOne() throws Exception {
        FieldAttrEntity fieldAttrEntity = InitExplainsHelper.getNewFieldAttrEntity();
        fieldAttrEntity.setAttr("text_1");
        fieldAttrEntity.setAttrName("关联业务单选");
        fieldAttrEntity.setSaasAttr("customer");
        fieldAttrEntity.setFieldType(20001);
        fieldAttrEntity.setSetType(0);
        fieldAttrEntity.setShowType(0);
        fieldAttrEntity.setEditHide(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setIcon(DemoIconEnum.getIconStr(0));
        fieldAttrEntity.setAttrType("");
        fieldAttrEntity.setLinkedType(100);
        XbbResponse<FieldAttrEntity> response = new XbbResponse<>(fieldAttrEntity);
        return JSON.toJSONString(response);
    }

}
